﻿
using Newtonsoft.Json;
using RTW.OA.Areas.Meeting.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.Meeting.Controllers
{
    public class EquipmentController : Controller
    {
        private EquipmentServers equipmentServers;

        public EquipmentController()
        {
            equipmentServers = new EquipmentServers();
        }


        // GET: Meeting/Equipment
        public ActionResult Index()
        {
            ViewBag.MR_IDData = JsonConvert.SerializeObject(equipmentServers.GetMR_ID());
            return View();
        }

        public ActionResult EquipmentDetails(int? id)
        {
            ViewBag.Title = "会议室设备明细";
            var model = new meeting_equipment();
            model = GetModel(id.Value.ToString());
            if (!string.IsNullOrEmpty(model.MR_ID.ToString()))
            {
                ViewBag.MR_ID = equipmentServers.GetMeetingById(model.MR_ID).MR_NAME;
            }
            else
            {
                ViewBag.MR_ID = "";
            }
            return View(model);
        }

        // GET: Meeting/Equipment/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.MR_IDData = equipmentServers.GetMR_ID();
            var model = new meeting_equipment();
            if (id != null)
            {
                ViewBag.Title = "编辑会议室设备表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建会议室设备表";
            }
            return View(model);
        }

        // POST: Meeting/Equipment/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, meeting_equipment model)
        {
            ViewBag.MR_IDData = equipmentServers.GetMR_ID();
            try
            {
                if (id == null || id == 0)
                {
                    equipmentServers.Add(model);
                    ViewBag.msg = "新增会议室设备表信息成功！";
                    model = new meeting_equipment();

                }
                else
                {
                    equipmentServers.Update(model);
                    ViewBag.msg = "修改会议室设备表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Meeting/Equipment/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                equipmentServers.Del(id.ToString());
                ViewBag.msg = "删除会议室设备表信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult(
                                 )
        {
            var list = equipmentServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public meeting_equipment GetModel(string id)
        {
            var model = equipmentServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}